IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CreateAdmSponsor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CreateAdmSponsor]
GO

set ANSI_NULLS ON
GO
set QUOTED_IDENTIFIER ON
GO


/******************************************************************************
**		VSS File: $/CreateAdmSponsor.sql 
**		Name: CreateAdmSponsor    
**		Desc: 	This procedure creates and updates a record in the 
**				AdmSponsor table
**              
**		Return values: None
** 
**		Called by:  SponsorAddition page 
**    
*******************************************************************************/
CREATE Procedure [dbo].[CreateAdmSponsor] (
	/* Param List */
	@name				VARCHAR(75),
	@shortName			VARCHAR(25),
	@description		VARCHAR(100),
	@admSponsorTypeId	INT,
	@rexSponsorId		INT = NULL,
	@sponsorId			INT OUTPUT,
	@InfoEdSponCode		VARCHAR(5) = NULL,
	@PSCustCode			VARCHAR(100) = NULL
)	   
AS    

BEGIN 

	SET NOCOUNT ON

	DECLARE @_errorCode INT
	SET @_errorCode = 0

	-- If 0 is given for a sponsorId, then it's an insert (new sponsor)
	IF (@sponsorId = 0)
	BEGIN 

	INSERT INTO AdmSponsor (
								Name, 
								ShortName,
								Description,
								AdmSponsorTypeId,
								DateCreated, 
								RexSponsorId,
								InfoEdSponCode,
								PSCustCode ) 
	VALUES
							 ( @name,	
								@shortName,
								@description,
								@admSponsorTypeId,
								GETDATE(),
								@rexSponsorId,	
								@InfoEdSponCode,
								@PSCustCode )

      SET @_errorCode = @@ERROR
		set @sponsorId=IDENT_CURRENT('AdmSponsor')

	END 
	ELSE
	BEGIN 
	--If the sponsor already exists update that record 
		UPDATE	AdmSponsor SET 
								Name = @name, 							
								ShortName = @shortName ,
								Description = @description, 
								AdmSponsorTypeId = @admSponsorTypeId,
								DateCreated = GETDATE(),  
								RexSponsorId = @rexSponsorId,
								InfoEdSponCode = @InfoEdSponCode,
								PSCustCode = @PSCustCode 
		WHERE Id = @sponsorId

      SET @_errorCode = @@ERROR
	END 

   IF @_errorCode <> 0 BEGIN
           RAISERROR('Unable to create AdmSponsor record for AdmSponsor %s', 16, 1, @name)
           RETURN @_errorCode
   END

	SELECT @sponsorId
END
GO
